//
//  RecordChangeAlertView.m
//  HLRecord
//
//  Created by XTW on 2023/1/18.
//

#import "RecordChangeAlertView.h"
#import "RecordChangeCollectionViewCell.h"
#import "UIView_extension.h"
#import "RecordViewController.h"

@interface RecordChangeAlertView()<UICollectionViewDelegate, UICollectionViewDataSource> {
    NSArray *dataArr;
    NSInteger selectRow;
}
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIView *showView;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@end

@implementation RecordChangeAlertView

- (instancetype)initCustomView {
    self = [[[NSBundle mainBundle] loadNibNamed:@"RecordChangeAlertView" owner:self options:nil] lastObject];
    if (self) {
        [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE)];
        [self.showView setFrame:CGRectMake(0, SCREEN_HEIGHT_DEVICE, SCREEN_WIDTH_DEVICE, 280+IS_iPhoneX_Bottom)];
        [self.showView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(12, 12)];

        [self.sureBtn setBackgroundImage:getColorToImage(rgba(236, 94, 101, 1)) forState:UIControlStateNormal];
        dataArr = [NSArray arrayWithObjects:@"萝莉", @"惊悚", @"大叔", @"搞笑", @"空旷", @"机器人", @"合唱", nil];
        selectRow = 0;
        [self configCollectionView];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView reloadData];
        
        [UIView animateWithDuration:0.3 animations:^{
            [self.showView setFrame:CGRectMake(0, SCREEN_HEIGHT_DEVICE - self.showView.frame.size.height, SCREEN_WIDTH_DEVICE, self.showView.frame.size.height)];
        }];
    }
    return self;
}

- (IBAction)closePress {
    [self removeFromSuperview];
}

- (IBAction)surePress {
    [self closePress];
    RecordViewController *viewCon = [[RecordViewController alloc] initWithNibName:@"RecordViewController" bundle:nil];
    viewCon.typeDisplay = [dataArr objectAtIndex:selectRow];
    CustomNavigationCoutroller *navCon = [[CustomNavigationCoutroller alloc] init];
    navCon.viewControllers = @[viewCon];
    navCon.modalPresentationStyle = UIModalPresentationFullScreen;
    [[UIViewControllerCJHelper findCurrentShowingViewController].navigationController presentViewController:navCon animated:YES completion:nil];
}

- (void)configCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(54, 54);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(0, 30, 0, 30);
    layout.minimumInteritemSpacing = 30;
    layout.minimumLineSpacing = 35;
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerClass:[RecordChangeCollectionViewCell class] forCellWithReuseIdentifier:@"RecordChangeCell"];
}

#pragma mark UICollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * CellIdentifier = @"RecordChangeCell";
    RecordChangeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    [cell showContent:indexPath.row+1 selectRow:selectRow+1 name:[dataArr objectAtIndex:indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    selectRow = indexPath.row;
    [self.collectionView reloadData];
}
@end
